package com.atguigu.mqdemo.provider.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Configuration
public class RabbitmqConfig {
    @Resource
    RabbitTemplate rabbitTemplate;
    @PostConstruct //java提供的 标注的方法会在构造器执行后立即执行
    public void init(){
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            //消息是否到达交换机的回调
            System.out.println(ack ? "(init)消息到达交换机" : ("(init)消息未到达交换机:" + cause));
        });
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            //消息未达到队列的回调
            System.out.println("(init)消息未到达队列：replyCode = " + replyCode
                    + " , replyText = " + replyText + " , exchange = " + exchange + " , routingKey = " + routingKey);
        });
    }
    @Bean
    public Exchange demo1Exchange(){
        return ExchangeBuilder.topicExchange("demo1.exchange")
                    .durable(true)
                    .ignoreDeclarationExceptions()
                    .build();
    }
    @Bean
    public Queue demo1Queue(){
        return QueueBuilder.durable("demo1.queue")
                    .build();
    }
    @Bean
    public Binding demo1Binding(Exchange demo1Exchange ,Queue demo1Queue ){
        return BindingBuilder.bind(demo1Queue)
                    .to(demo1Exchange)
                // * 通配一个一级的任意多个字符  # 通配任意多级多个字符
                    .with("a.*")
                    .noargs();

    }

}
